16 Lecture
CS201
Midterm & Final Term Short Notes
Pointers (continued)
Pointers in programming are variables that store memory addresses, i.e., the location of other variables in memory. Pointers are used to manipulate and reference memory locations, and their manipulation is fundamental to programming in languages
Important Mcq's
Midterm & Finalterm Prepration
Past papers included
Download PDF
What is the value of the pointer variable ptr after the following code is executed?
arduinoint *ptr; int x = 5; ptr = &x;
a) 0 b) 5 c) Memory address of x d) Error
Answer: c) Memory address of x
Which operator is used to access the value at a memory location pointed to by a pointer? a) & b) * c) $ d) #
Answer: b) *
What is the purpose of a null pointer? a) It points to the memory location containing 0. b) It points to the memory location containing NULL. c) It is a pointer that has not been initialized. d) It is a pointer that points to the beginning of an array.
Answer: b) It points to the memory location containing NULL.
What is the output of the following code?
perlint arr[3] = {1, 2, 3}; int *ptr = &arr[1]; printf("%d", *ptr);
a) 1 b) 2 c) 3 d) Error
Answer: b) 2
What is the output of the following code?
perlint arr[3] = {1, 2, 3}; int *ptr = arr; printf("%d", *(ptr + 2));
a) 1 b) 2 c) 3 d) Error
Answer: c) 3
What is the difference between a pointer and a reference? a) A pointer is a variable that holds the memory address of another variable, while a reference is an alias for another variable. b) A pointer is a reference to another variable, while a reference is a variable that holds the memory address of another variable. c) There is no difference between a pointer and a reference. d) A pointer can be used with any data type, while a reference is limited to specific data types.
Answer: a) A pointer is a variable that holds the memory address of another variable, while a reference is an alias for another variable.
What is the purpose of the & operator in pointer operations? a) It returns the memory address of a variable. b) It dereferences a pointer variable. c) It assigns a value to a pointer variable. d) It tests a pointer for a null value.
Answer: a) It returns the memory address of a variable.
What is a dangling pointer? a) A pointer that points to a memory location that has been freed. b) A pointer that points to a null value. c) A pointer that has been assigned an incorrect memory address. d) A pointer that points to the beginning of an array.
Answer: a) A pointer that points to a memory location that has been freed.
What is the output of the following code?
perlint arr[3] = {1, 2, 3}; int *ptr = arr; *ptr = 4; printf("%d", arr[0]);
a) 1 b) 2 c) 3 d) 4
Answer: d) 4
What is the purpose of pointer arithmetic? a) To perform arithmetic operations on the memory locations pointed to by pointers. b) To perform arithmetic operations on the values
Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included
Download PDF
- What is a pointer variable? Give an example of declaring a pointer variable in C. Answer: A pointer variable is a variable that stores the memory address of another variable. Example of declaring a pointer variable in C:
arduinoint *ptr;
What is the difference between declaring a pointer variable and initializing a pointer variable? Answer: Declaring a pointer variable means specifying its type and name. Initializing a pointer variable means assigning a value (i.e., a memory address) to it.
What is the difference between pointer arithmetic and normal arithmetic? Answer: Pointer arithmetic is performed on memory addresses, whereas normal arithmetic is performed on values. In pointer arithmetic, the value of a pointer is incremented or decremented based on the size of the data type that it points to.
How do you allocate memory dynamically using the malloc() function in C? Answer: The malloc() function is used to dynamically allocate memory in C. It takes one argument, which is the size of the memory block to be allocated (in bytes). For example:
cint *ptr = (int *) malloc(sizeof(int));
What is a null pointer in C? Answer: A null pointer is a pointer that does not point to any memory address. In C, it is represented by the value 0 or NULL.
What is a function pointer in C? Give an example of declaring a function pointer. Answer: A function pointer is a pointer that points to the memory address of a function. Example of declaring a function pointer:
cint (*ptr)(int, int);
What is the difference between a void pointer and a null pointer? Answer: A void pointer is a pointer that points to memory of any data type, whereas a null pointer is a pointer that does not point to any memory address.
How do you use the "->" operator in C to access a member of a structure through a pointer? Answer: The "->" operator is used to access a member of a structure through a pointer. For example:
arduinostruct student {
char name[50];
int age;
};
struct student *ptr;
ptr->age = 20;
- What is a double pointer in C? Give an example of declaring a double pointer. Answer: A double pointer is a pointer that points to the memory address of another pointer. Example of declaring a double pointer:
arduinoint **ptr;
- What is the difference between passing a variable by value and passing it by reference in a function call? Answer: When a variable is passed by value, a copy of its value is passed to the function, whereas when it is passed by reference, a reference to its memory address is passed to the function. This means that any changes made to the variable inside the function will affect the original variable when passed by reference, but not when passed by value.